/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Compras;

import Controlador.Bean.Almacen.Producto;
import Controlador.Bean.Compras.CatalogoData;
import Controlador.Bean.Compras.ProductoxProveedor;
import Controlador.Bean.Compras.Proveedor;
import gui.General.Main;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import lib.Validaciones;
import Atxy2k.CustomTextField.RestrictedTextField;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author LiziTa
 */


public class agregarCatalogo extends javax.swing.JInternalFrame {

   
    /**
     *
     * @param prov
     * @param prod
     */
    private JInternalFrame parent;
   
    public void filtrar(String prov, String prod){
        this.prov = prov;
        this.prod = prod;
        tb.actualizarTabla2();
    }
    
    int clic=0;
    String selectedId;
    String prov="";
    String prod="";
     Integer here;
    public String proveedor;
    public String producto;
       
    private TableModel tb;
   public List<Proveedor> proveedores=Controlador.Service.Compras.GestionCompra.obtenerProveedor();
    public List<ProductoxProveedor> proxprov;
    public List<Producto> productos=Controlador.Service.Compras.GestionCompra.seleccionarMatPrima();
   
    
    public agregarCatalogo() {
               initComponents();
               initComp();
//               
//                TableRowSorter<TableModel> sorter  = new TableRowSorter<TableModel>((TableModel) tblCatalogo.getModel());
//         tblCatalogo.setRowSorter(sorter);
//              
    }

     private static agregarCatalogo myoc=null;
   
    
  public static agregarCatalogo getInstance() {
         if(myoc == null){
                       myoc = new agregarCatalogo();    
            Main.getDesktop().add(myoc); 
            myoc.setVisible(true);
               myoc.toFront();           
                    }
                    else{
                        if (myoc.isClosed()|| !myoc.isVisible()) {
                                myoc = new agregarCatalogo(); 
                                Main.getDesktop().add(myoc);
                                myoc.toFront();
                                myoc.setVisible(true);
                        }
                        else
                            myoc.toFront();
                    }
        
        
        
                return myoc;
    }
    
     private void initComp() {
          
         
        tb = new TableModel();
     tblCatalogo.setModel(tb);
tblCatalogo.setRowSelectionAllowed(true);

txtId.setEnabled(false);
txtId.setVisible(false);
RestrictedTextField r = new RestrictedTextField(txtleadtime);
             r.setLimit(10);
             
             
RestrictedTextField r1 = new RestrictedTextField(txtFactor);
             r1.setLimit(5);
             
RestrictedTextField r2 = new RestrictedTextField(txtpbase);
             r2.setLimit(10);             
             
 
       tblCatalogo.setDefaultRenderer(JButton.class, new agregarCatalogo.MiRender());
       tblCatalogo.setDefaultEditor( JButton.class, new agregarCatalogo.MiEditor());
       tb.actualizarTabla();
           
    

     }



    
     class TableModel extends AbstractTableModel{
        
        //Two arrays used for the table data
       String[] columnNames = {"Proveedor","Producto","IdProveedor","IdProducto", "Tiempo entrega", "Precio", "Factor", "Eliminar"};
               
      
           public List<ProductoxProveedor> data1 = Controlador.Service.Compras.GestionCompra.obtenerProdxProv(prov,prod);
            public List<CatalogoData> data2=Controlador.Service.Compras.GestionCompra.seleccionarCatalogo(prov, prod);
           
           public void actualizarTabla(){
               //data1 = Controlador.Service.Compras.GestionCompra.obtenerProdxProv(prov,prod);
               data2=Controlador.Service.Compras.GestionCompra.seleccionarCatalogo(prov, prod);
           fireTableDataChanged();           
        } 
           
          public void actualizarTabla2(){
                data2 = Controlador.Service.Compras.GestionCompra.filtrarProdxProv(prov,prod);
                fireTableDataChanged();
                prov="";
            prod="";
        }
         
        
        @Override
        public int getRowCount()
        {
         
            return data2.size();
        }
        
        
        
        @Override
        public int getColumnCount()            
        {
            return columnNames.length;
        }
        
        @Override
        public Object getValueAt(int row, int column)
        {   
            switch(column){
                case 0:return data2.get(row).getRazonsocial();
                case 1: return data2.get(row).getNombre();
                case 2: return data2.get(row).getIdProveedor();
                case 3: return data2.get(row).getIdProducto();
                case 4: return data2.get(row).getLeadtime();
                case 5: return data2.get(row).getPreciobase();
                case 6: return data2.get(row).getFactorcalidad();
                default: return new JButton("Eliminar");
            }
        }
        
        //Used by the JTable object to set the column names
        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }
        
        //Used by the JTable object to render different
        //functionality based on the data type
        @Override
        public Class getColumnClass(int c) {
            if(c!=7){
            return getValueAt(0, c).getClass();
            }else{
                return JButton.class;
            }
        }
        
        @Override
        public boolean isCellEditable(int row, int column)
        {
	        return false;
        }

       
     }
     
     
     public class MiEditor extends AbstractCellEditor implements TableCellEditor {
         private Boolean currentValue;
         @Override
         public Object getCellEditorValue()
         {
            return currentValue;
         }
         //El editor usara el propio componente. Para que funcione la celda en el modelo debe ser editable.
         @Override
         public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
         {
           return (JComponent) value;// la tabla solo debe tener componentes graficos
         }
     }

      public class MiRender implements TableCellRenderer {
        // Este metodo indica como debe de pintarse el elemento en la fila row, en la columna column
        // que esta en la tabla table y tiene el color dado por el objeto.
        @Override
         public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus, int row, int column) {    
            // como el boton y el calendario son componentes esto se vale.
            return (JComponent)table.getValueAt(row, column);
        }
     }  
  
    
    public boolean valido(){
    boolean b =true;
        
    boolean factor = Validaciones.validarRealMg(txtFactor.getText());
        boolean precioBase = Validaciones.validarRealMg(txtpbase.getText());
        boolean tiempo = Validaciones.validarEntero(txtleadtime.getText());
    //    if(factor && precioBase && tiempo && !txtleadtime.getText().isEmpty() && cmbProducto.getSelectedIndex() != 0 && cmbProveedor.getSelectedIndex() != 0){
    String s1="";
    String s2="";
    String s3="";
    String s4="";
    String s5="";
    String s6="";
    String s7="";
        
     if(txtFactor.getText().isEmpty() || txtpbase.getText().isEmpty() || txtleadtime.getText().isEmpty()){
            b = false;
           s1="Los campos no deben estar vacíos";
        }
    
     if(txtProveedor.getText().isEmpty()  || txtIdProv.getText().isEmpty()) {
            b = false;
           s2="Debe seleccionar un proveedor";
        }
     
     if(txtProducto.getText().isEmpty() || txtIdProd.getText().isEmpty()) {
            b = false;
           s3="Debe seleccionar un producto";
        }
        
        if( !txtFactor.getText().isEmpty()&& !factor ){
            b = false;
           s4="Calificacion no válida";
        }
         if( factor && (Double.parseDouble(txtFactor.getText())>1.0 || Double.parseDouble(txtFactor.getText())<0.0)){
            b = false;
           s5="Calificacion debe ser valor mayor a 0 y menor que 1";
        }
               
        if(!txtpbase.getText().isEmpty()&& !precioBase ){
            b = false;
           s6="Precio no válido";
        }
        
        if(!txtleadtime.getText().isEmpty()&&!tiempo ){
            b = false;
            s7="Tiempo no válido";
        }
       
      String rpta="";
         
         if (s1!="") rpta = rpta+ s1 + "\n";
         if (s2!="") rpta = rpta+ s2 + "\n";
         if (s3!="") rpta = rpta+ s3 + "\n";
         if (s4!="") rpta = rpta+ s4 + "\n";
         if (s5!="") rpta = rpta+ s5 + "\n";
         if (s6!="") rpta = rpta+ s6 + "\n";
         if (s7!="") rpta = rpta+ s7 + "\n";
                
                
         if (rpta!="")
         JOptionPane.showMessageDialog(null, "Errores: \n" + rpta );
            
        
        
        return b;
    }
   
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtleadtime = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtpbase = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtFactor = new javax.swing.JTextField();
        txtId = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        txtProducto = new javax.swing.JTextField();
        txtProveedor = new javax.swing.JTextField();
        txtIdProd = new javax.swing.JTextField();
        txtIdProv = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblCatalogo = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel7.setText("jLabel7");

        setClosable(true);
        setIconifiable(true);
        setTitle("Catalogo");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Catalogo por Proveedor"));

        jLabel2.setText("Producto");

        jLabel4.setText("Tiempo entrega");

        jLabel8.setText("Precio");

        txtpbase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtpbaseActionPerformed(evt);
            }
        });

        jLabel1.setText("Proveedor");

        jLabel3.setText("Calificacion");

        jLabel5.setText("(dias)");

        jLabel9.setText("(soles)");

        jLabel10.setText(" 0 < Rango<1");

        jButton4.setText("Seleccionar Producto");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Seleccionar Proveedor");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        txtProducto.setEditable(false);
        txtProducto.setEnabled(false);

        txtProveedor.setEditable(false);
        txtProveedor.setEnabled(false);

        txtIdProd.setEditable(false);
        txtIdProd.setEnabled(false);

        txtIdProv.setEditable(false);
        txtIdProv.setEnabled(false);

        jLabel11.setText("IdProducto");

        jLabel12.setText("IdProveedor");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel12))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton4)
                                    .addComponent(txtProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtIdProd, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(txtleadtime, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel5)))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jLabel6)
                                                .addGap(104, 104, 104)))
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel1))))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtFactor, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel10)
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton5)
                    .addComponent(txtProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtIdProv, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtpbase, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel9)
                .addGap(14, 14, 14))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtIdProd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(txtIdProv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(txtProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jButton4)
                    .addComponent(jButton5))
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtleadtime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtpbase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel4))
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtFactor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jButton1.setText("Guardar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Descartar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        tblCatalogo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblCatalogo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCatalogoMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblCatalogo);

        jButton3.setText("Buscar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(172, 172, 172)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2)))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addGap(32, 32, 32)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtpbaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtpbaseActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtpbaseActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
   if (valido()) {       
            ProductoxProveedor pxp = new ProductoxProveedor(txtIdProd.getText(),txtIdProv.getText(),"0",Double.parseDouble(txtpbase.getText()),Double.parseDouble(txtFactor.getText()), Integer.parseInt(txtleadtime.getText()),"1");
            
            
            
            if (txtId.getText().isEmpty()) {
                
                Boolean existeproducto=Controlador.Service.Compras.GestionCompra.existeproducto(txtIdProd.getText(),txtIdProv.getText());
                if (!existeproducto) { 
                    Controlador.Service.Compras.GestionCompra.agregarProdxProv(pxp);
                 JOptionPane.showMessageDialog(null,"Datos agregados");
                }
                else JOptionPane.showMessageDialog(null,"No se puede ingresar datos repetidos");
            
            }
            else if (!txtId.getText().isEmpty() || clic>0 || here==0  )
            { Boolean existeproducto=Controlador.Service.Compras.GestionCompra.existeproducto(producto,proveedor);
                if (!existeproducto) { 
                    Controlador.Service.Compras.GestionCompra.actualizarProdxProv(pxp,producto,proveedor);
                  txtId.setText("");
                    JOptionPane.showMessageDialog(null,"Datos actualizados");
                }
                else JOptionPane.showMessageDialog(null,"No se puede actualizar con dichos valores porque ya existen ");
                
                
                
           
             
            }
            
            
               txtpbase.setText("");
            txtleadtime.setText("");
            txtFactor.setText("");
             txtIdProv.setText("");
           txtIdProd.setText("");
             txtProducto.setText("");
           txtProveedor.setText("");
            tb.actualizarTabla();
           
        
       }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

         txtpbase.setText("");
            txtleadtime.setText("");
            txtFactor.setText("");
             txtIdProv.setText("");
           txtIdProv.setText("");
             txtProducto.setText("");
           txtProveedor.setText("");
        
                
    }//GEN-LAST:event_jButton2ActionPerformed

    private void tblCatalogoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCatalogoMouseClicked
        // TODO add your handling code here:
         if(evt.getClickCount()==2) here=0; else here=1;
      
        ArrayList<String> prov;
        txtId.setText("A");
        int selectedRowIndex = tblCatalogo.getSelectedRow();
        int selectedColumnIndex = tblCatalogo.getSelectedColumn();
        if(selectedColumnIndex!=7){
           String selectId = (String) tblCatalogo.getValueAt(selectedRowIndex, 2);
//
            if(clic == 1 && selectedId.compareTo(selectId)==0){
             proveedor=((String)tblCatalogo.getValueAt(selectedRowIndex, 2));
              producto=((String)tblCatalogo.getValueAt(selectedRowIndex, 3));
                txtleadtime.setText((tblCatalogo.getValueAt(selectedRowIndex, 4)).toString());
                txtFactor.setText((tblCatalogo.getValueAt(selectedRowIndex, 6)).toString());
                txtpbase.setText((tblCatalogo.getValueAt(selectedRowIndex, 5)).toString());
                txtIdProd.setText((tblCatalogo.getValueAt(selectedRowIndex, 3)).toString());
                txtIdProv.setText((tblCatalogo.getValueAt(selectedRowIndex, 2)).toString());
                txtProveedor.setText((tblCatalogo.getValueAt(selectedRowIndex, 0)).toString());
                txtProducto.setText((tblCatalogo.getValueAt(selectedRowIndex, 1)).toString());
                clic = 0;
               
            }else if(clic == 0){
                selectedId = selectId;
                clic++;
            }else{
                clic = 0;
            }
        }else{
            int confirmado = JOptionPane.showConfirmDialog(this,"¿Desea eliminar el producto del proveedor?");
            if (confirmado == JOptionPane.OK_OPTION){
                
                 int cond = Controlador.Service.Compras.GestionCompra.eliminarProdxProv((String)tblCatalogo.getValueAt(selectedRowIndex, 2),(String)tblCatalogo.getValueAt(selectedRowIndex, 3));
               if(cond == 1){
                    JOptionPane.showMessageDialog(this, "El proveedor que desea eliminar está siendo utilizado");
                } tb.actualizarTabla();
                
              
            }
        }

        
    }//GEN-LAST:event_tblCatalogoMouseClicked

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
  BuscarCatalogo bc = new BuscarCatalogo(this);
        Main.getDesktop().add(bc);
        bc.setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
          CatalogoSeleccionarProveedor buscarProveedor = new CatalogoSeleccionarProveedor();
          Main.getDesktop().add(buscarProveedor);
          buscarProveedor.setVisible(true);
        
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
         CatalogoSeleccionarProducto buscarProducto = new CatalogoSeleccionarProducto();
          Main.getDesktop().add(buscarProducto);
          buscarProducto.setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    public javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable tblCatalogo;
    private javax.swing.JTextField txtFactor;
    private javax.swing.JTextField txtId;
    public static javax.swing.JTextField txtIdProd;
    public static javax.swing.JTextField txtIdProv;
    public static javax.swing.JTextField txtProducto;
    public static javax.swing.JTextField txtProveedor;
    private javax.swing.JTextField txtleadtime;
    public static javax.swing.JTextField txtpbase;
    // End of variables declaration//GEN-END:variables
}